1.3 Input and Output in C
Module 1.3 • Standard I/O Streams, Formatted Terminal I/O & Escape Sequences
Every computer program generally performs three main tasks:
- Accepts data from the user (Input)
- Processes the data (Processing)
- Displays the result (Output)
For example, a calculator program takes two numbers as input, performs calculations, and displays the answer as output.
Why Input and Output Are Important
Without input, a program cannot receive data from users. Without output, users cannot see the results produced by the program. Input and output operations help programs interact with users.
Standard Input and Output Library
C language provides input and output operations through library functions. These functions are available in the Standard Input/Output Library.
To use them, we include the header file:
#include <stdio.h>
This header file contains commonly used functions such as:
- scanf()
- printf()
- getchar()
- putchar()
Common Input and Output Functions
| Function | Purpose |
|---|---|
scanf() | Reads formatted input |
printf() | Displays formatted output |
getchar() | Reads a single character |
putchar() | Displays a single character |
1.3.1 Conversion Specifiers
Conversion specifiers tell the compiler what type of data is being read or displayed. Every conversion specifier starts with %.
Frequently Used Specifiers
| Specifier | Meaning |
|---|---|
%d | Integer |
%f | Float |
%lf | Double |
%c | Character |
%s | String |
%u | Unsigned Integer |
%o | Octal Number |
%x | Hexadecimal Number |
Examples
%d
%f
%c
%s
1.3.2 Reading Input Using scanf()
The scanf() function is used to take input from the keyboard.
Syntax
scanf("format_specifier", &variable);
The & symbol is called the address operator. It tells the compiler where the entered value should be stored.
Reading an Integer
#include <stdio.h>
int main()
{
int age;
scanf("%d", &age);
return 0;
}
Input
28
The value 28 is stored in the variable age.
Reading a Floating Point Number
float salary;
scanf("%f", &salary);
Input
45875.50
Reading a Character
char grade;
scanf("%c", &grade);
Input
A
Reading a String
char city[20];
scanf("%s", city);
Input
Chennai
Notice that strings do not require the & operator.
Reading Multiple Values
More than one value can be entered using a single scanf() statement.
int id;
float marks;
scanf("%d %f", &id, &marks);
Input
101 87.50
Stored values:
id = 101
marks = 87.50
Using Special Delimiters
Input values can also be separated using special symbols.
Colon Separator
int hours;
float rate;
scanf("%d:%f", &hours, &rate);
Input
8:250.75
Result:
hours = 8
rate = 250.75
Comma Separator
int x;
float y;
scanf("%d,%f", &x, &y);
Input
50,75.25
Date Format Example
int day, month, year;
scanf("%d-%d-%d", &day, &month, &year);
Input
15-08-2026
Result:
day = 15
month = 8
year = 2026
1.3.3 Displaying Output Using printf()
The printf() function is used to display data on the screen.
Syntax
printf("format", variables);
Displaying Simple Text
printf("Welcome to C Programming");
Output
Welcome to C Programming
Displaying Integer Values
int age = 30;
printf("%d", age);
Output
30
Displaying Float Values
float temperature = 32.8;
printf("%f", temperature);
Output
32.800000
Displaying Character Values
char section = 'B';
printf("%c", section);
Output
B
Displaying String Values
char name[] = "Rahul";
printf("%s", name);
Output
Rahul
Mixing Text and Variables
int marks = 92;
printf("Student Marks = %d", marks);
Output
Student Marks = 92
Displaying Multiple Variables
int id = 201;
float salary = 55000.75;
char grade = 'A';
printf("ID=%d Salary=%f Grade=%c", id, salary, grade);
Output
ID=201 Salary=55000.750000 Grade=A
Escape Sequences
Escape sequences provide special formatting effects.
New Line (\n)
printf("Java\nPython\nC");
Output
Java
Python
C
Horizontal Tab (\t)
printf("Name\tMarks");
Output
Name Marks
Backslash (\\)
printf("D:\\Projects\\CPrograms");
Output
D:\Projects\CPrograms
Double Quotes (\")
printf("She said, \"Good Morning\"");
Output
She said, "Good Morning"
1.3.4 Formatted Input and Output
Formatted input and output help control how data is entered and displayed.
Width Specifier for Integer Output
int num = 45;
printf("%5d", num);
Output
45
The number is right-aligned within a field width of 5.
Multiple Integer Formatting
int a = 25;
int b = 7;
printf("%4d %4d", a, b);
Output
25 7
Floating Point Formatting
By default, six digits appear after the decimal point.
float value = 45.6789;
printf("%f", value);
Output
45.678900
Controlling Decimal Places
float value = 45.6789;
printf("%.2f", value);
Output
45.68
Width and Precision Together
float amount = 125.4567;
printf("%8.2f", amount);
Output
125.46
String Formatting
printf("%.4s", "Programming");
Output
Prog
Only the first four characters are displayed.
1.3.5 Suppression Character in scanf()
Sometimes we want to skip certain input values. The * symbol is used for this purpose.
Example
int a, b;
scanf("%d %*d %d", &a, &b);
Input
100 200 300
Result:
a = 100
b = 300
The value 200 is ignored.
1.3.6 Character Input and Output
For reading and displaying a single character, C provides:
- getchar()
- putchar()
Using getchar()
char ch;
ch = getchar();
Reads one character from the keyboard.
Using putchar()
putchar(ch);
Displays one character on the screen.
Example Program
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character: ");
ch = getchar();
printf("You entered: ");
putchar(ch);
return 0;
}
Input
K
Output
You entered: K
Complete Example Program
#include <stdio.h>
int main()
{
int employeeId;
float salary;
char grade;
printf("Enter Employee ID: ");
scanf("%d", &employeeId);
printf("Enter Salary: ");
scanf("%f", &salary);
printf("Enter Grade: ");
scanf(" %c", &grade);
printf("\nEmployee Details\n");
printf("ID = %d\n", employeeId);
printf("Salary = %.2f\n", salary);
printf("Grade = %c\n", grade);
return 0;
}
Summary
- Input allows users to provide data to a program.
- Output displays results on the screen.
- scanf() is used to read formatted input.
- printf() is used to display formatted output.
- Conversion specifiers such as %d, %f, %c, and %s define data types.
- Escape sequences help format output.
- Width and precision specifiers improve output presentation.
- getchar() and putchar() are used for single-character input and output.
- Formatted I/O makes programs easier to use and more professional.
Click your choice for each question to view feedback immediately. Complete all questions to evaluate your metric score.